package com.hua.nailcampus.acl.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hua.nailcampus.acl.model.Avatar;
import com.hua.nailcampus.acl.model.User;
import com.hua.nailcampus.acl.model.vo.UserVo;
import com.hua.nailcampus.acl.service.UserService;
import com.hua.nailcampus.utils.JwtHelper;
import com.hua.nailcampus.utils.Result;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @Author:chenjianhua
 * @Date:2023/11/17 16:14
 * @Version 1.8
 */
@Slf4j
@RestController
@RequestMapping("nailCampus/user")
public class UserController {

    @Autowired
    private UserService userService;


    /**
     *获取分页列表
     * @return
     */
    @GetMapping("web/getAllUser/{current}/{limit}")
    public Result getAllUser(@PathVariable Integer current,
                             @PathVariable Integer limit,
                             User user){
        IPage<UserVo> pageModel = userService.getAllUser(current,limit,user);
        return Result.ok(pageModel);
    }
    @PostMapping("web/adminUpdateUser")
    public Result adminUpdateUser(@RequestBody UserVo userVo){
        userService.UpdateRole(userVo);
        return Result.ok(null);
    }

    /**
     * 根据token获取用户信息
     * @param request
     * @return
     */
    @GetMapping("/getUser")
    public Result getUser(HttpServletRequest request){
        String token = request.getHeader("token");
        log.info(token);
        Integer userId = JwtHelper.getUserId(token);
        if(userId==null){
            return Result.fail("未登录,请进行登录");
        }
        UserVo userVo = userService.getUser(userId);
        return Result.ok(userVo);
    }

    /**
     * 根据ID查询用户信息
     * @param id
     * @return
     */
    @GetMapping("/getUserById/{id}")
    public Result getUserById(HttpServletRequest request, @PathVariable Integer id){
        Integer userId = JwtHelper.getUserId(request.getHeader("token"));
        UserVo userVo = userService.getUserById(userId,id);
        return Result.ok(userVo);
    }

    /**
     * 更新用户数据
     * @param userVo
     * @return
     */
    @PostMapping("/updateUser")
    public Result updateUser(@RequestBody UserVo userVo){
        log.info("获取到的UserVo:{}",userVo);
        userService.updateUser(userVo);
        return Result.ok(null);
    }

    /**
     * 查找好友用户信息
     * @param phone
     * @return
     */
    @GetMapping("/searchByPhone/{phone}")
    public Result addFriend(@PathVariable String phone){
        UserVo userVo = userService.getUserByPhone(phone);
        if(userVo == null){
            return Result.fail("无法找到该用户");
        }
        return Result.ok(userVo);
    }

    @GetMapping("getRole")
    public Result getRole(HttpServletRequest request){
        String token = request.getHeader("token");
        Integer userId = JwtHelper.getUserId(token);
        Integer roleCode = userService.getUserRole(userId);
        return Result.ok(roleCode);
    }
}
